function js_main1(str1, str2) {
  if (str1.length !== str2.length) return false;
  const map = {};
  for (const char1 of str1) {
    map[char1] = !map[char1] ? 1 : map[char1] + 1;
  }
  for (const char2 of str2) {
    if (!map[char2]) return false;
    map[char2]--;
  }
  return true;
}

// 暴力解法，利用字符的ASCII码建立数组分析
function js_main2(str1, str2) {
  if (str1.length !== str2.length) {
    return false;
  }
  if (str1 === str2) {
    return true;
  }
  const table = Array(26).fill(0);
  const startCode = 'a'.codePointAt(0);
  for (let i = 0; i < str1.length; ++i) {
    table[str1.codePointAt(i) - startCode]++;
  }
  for (let i = 0; i < str2.length; ++i) {
    const index = str2.codePointAt(i) - startCode;
    table[index]--;
    if (table[index] < 0) {
      return false;
    }
  }
  return true;
}

console.log(js_main1('anagram', 'nagaram'));
console.log(js_main1('rat', 'cat'));

console.log(js_main2('anagram', 'nagaram'));
console.log(js_main2('rat', 'cat'));
